# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
import os

from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.files import copy, get
from conan.tools.gnu import AutotoolsToolchain, Autotools

required_conan_version = ">=2.13.0"


class FlexConan(ConanFile):
    name = "flex"
    license = "GPL V2.0"
    description = "The fast lexical analyzer generator"
    chost = None
    settings = "os", "compiler", "build_type", "arch"
    _autotools = None

    def source(self):
        get(self, **self.conan_data["sources"][self.version], strip_root=True)

    def generate(self):
        at = AutotoolsToolchain(self)
        at.generate()

    def build(self):
        # 作为工具只需要支持x86_64构建，其他arch构建会coredump
        if self.settings.arch != "x86_64":
            return
        autotools = self._configure_autotools()
        autotools.make()

    def package(self):
        # 作为工具只需要支持x86_64构建，其他arch构建会coredump
        if self.settings.arch != "x86_64":
            return
        autotools = self._configure_autotools()
        autotools.install()

    def package_info(self):
        # 作为工具只需要支持x86_64构建，其他arch构建会coredump
        if self.settings.arch != "x86_64":
            return
        self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))

    def _configure_autotools(self):
        if self._autotools:
            return self._autotools

        self._autotools = Autotools(self)
        self._autotools.configure()
        return self._autotools
